package com.spukmk3me.animatorpack.pack0;

import com.spukmk3me.math.Vector3f;
import com.spukmk3me.scene.Animator;
import com.spukmk3me.scene.Node;

public class ScaleAnimator extends Animator
{
    public void setupScale( float x0, float y0,
        float scaleStartX, float scaleStartY,
        float scaleFinishX, float scaleFinishY, int timeMS )
    {
        m_time = Math.max( 0, (timeMS << 16 ) / 1000 );
        m_scaleCenter = new Vector3f( x0, y0, 0 );
        m_startScale = new Vector3f( scaleStartX, scaleStartY, 1 );
        m_finishScale = new Vector3f( scaleFinishX, scaleFinishY, 1 );
        m_scaleSpeed = new Vector3f();
        
        if ( m_time != 0 )
        {
            m_scaleSpeed.set( m_finishScale );
            m_scaleSpeed.dec( m_startScale );
            m_scaleSpeed.mul( 1000f / timeMS );
        }
        
        if ( getNode() != null )
        {
            Node node = getNode();
            
            node.reset();
            node.setPivot( x0, y0 );
            node.setScale( scaleStartX, scaleStartY );
        }
        
        m_initialized = true;
    }
    

    @Override
    public void animate( int deltaTime )
    {
        if ( !m_initialized )
            return;
        
        if ( m_time > 0 )
        {
            float timeF = 1.0f * deltaTime / 0x00010000;
            
            m_startScale.x += timeF * m_scaleSpeed.x;
            m_startScale.y += timeF * m_scaleSpeed.y;
            m_time -= deltaTime;
        }
        
        Node node = getNode();
        
        node.reset();
        node.setPivot( m_scaleCenter.x, m_scaleCenter.y );
        
        if ( m_time > 0 )
            node.setScale( m_startScale.x, m_startScale.y );
        else
            node.setScale( m_finishScale.x, m_finishScale.y );
    }

    @Override
    public boolean isAnimating()
    {
        return m_time > 0;
    }
    
    @Override
    protected void onNodePrepareToChange( Node newNode ) {}
    
    @Override
    protected void onNodeChanged() {}    
    
    private Vector3f    m_startScale, m_finishScale, m_scaleSpeed, m_scaleCenter;
    private boolean     m_initialized;
    private int         m_time;
}
